package cn.aj.exm.web.controller;

import cn.aj.exm.service.IUserService;
import cn.aj.exm.domain.User;
import cn.aj.exm.query.UserQuery;
import cn.aj.exm.result.JsonResult;
import cn.aj.exm.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(userService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JsonResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JsonResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }

    /**
    * @Title: list
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2023/01/2 0002 22:01
    * @Parameters: []
    * @Return cn.aj.exm.result.JsonResult
     * 用户服务调电影服务 查询电影票
    */
    @RequestMapping(value = "/loadMovieTicket",method = RequestMethod.GET)
    public JsonResult loadMovieTicket(){
        return JsonResult.success(userService.loadMovieTicket());
    }

    /**
    * @Title: buyTicket
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2023/01/2 0002 23:18
    * @Parameters: []
    * @Return cn.aj.exm.result.JsonResult
    */
    @RequestMapping(value = "/buyTicket{movieId}/{count}",method = RequestMethod.GET)
    public JsonResult buyTicket(@PathVariable("movieId") Long movieId, @PathVariable("count")Integer count){
        return userService.buyTicket(movieId, count);
    }

    /**
    * @Title: saveOrUpdate
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2023/01/3 0003 0:06
    * @Parameters: [user]
    * @Return cn.aj.exm.result.JsonResult
     * 新增用户
    */
    @RequestMapping(value="/addUser",method= RequestMethod.POST)
    public JsonResult addUser(@RequestBody User user){
        return userService.addUser(user);
    }

    /**
    * @Title: buyTicket
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2023/01/3 0003 0:30
    * @Parameters: [movieId, count]
    * @Return cn.aj.exm.result.JsonResult
     * 看电影
    */
    @RequestMapping(value = "/watchMovie{movieId}/{count}",method = RequestMethod.GET)
    public JsonResult watchMovie(@PathVariable("movieId") Long movieId, @PathVariable("count")Integer count){
        return userService.watchMovie(movieId, count);
    }
}
